#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "QtDebug"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    TextShow = new QTextEdit(this);
    TextShow->setGeometry(0,0,this->width(),this->height());

    data = new CData("C:\\Users\\15259\\Documents\\51world\\resources\\TestData.csv");

    // udp 发送
    udpsend = new UdpSender();

    // 创建定时器
    timer = new QTimer(this);
    // 设置为重复触发
    timer->setSingleShot(false);
    // 设置触发时间为100ms
    timer->start(10);
    //定时器触发信号槽
    connect(timer, SIGNAL(timeout()), this, SLOT(TimerTimeOut()));
}

MainWindow::~MainWindow()
{
    delete data;
    delete timer;
    delete TextShow;
    delete ui;
}

void MainWindow::TimerTimeOut()
{
    if(udp != nullptr)
    {
        static int num = 0;
        TextShow->setText(udp->strclient_address + "\ncunt:" + QString::number(num));

        if(udp->m_data.size() > 0)
        {
            udpsend->initSender(udp->strclient_address, 5112);

            if(QString(udp->m_data.data()) == "connect")
            {
                num = 0;
            }
            else if(QString(udp->m_data.data()) == "count")
            {
                qDebug() << "count " << num;
                num++;
            }
            else if(QString(udp->m_data.data()) == "data")
            {
                if(num >= data->listdata.size()) num = data->listdata.size();
                if(data != nullptr) udpsend->send(data->listdata.at(num).toUtf8());
                else udpsend->send("error");
            }

            udp->m_data.clear();
        }
    }
}

void MainWindow::on_buttonGetData_clicked()
{

}
